package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.Coupon;
import com.woniuxy.dudugua.service.AdminService;
import com.woniuxy.dudugua.util.Page;
import com.woniuxy.dudugua.util.Result;
import com.woniuxy.dudugua.util.view.AdminOrderView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {
    @Resource
    private AdminService adminService;
    @RequestMapping("selectOrder")
    public Result selectOrder(Integer index,Integer number)throws Exception{
        List<AdminOrderView> adminOrderViews = adminService.selectAllOrder(index,number);
        Integer dataCount = Math.toIntExact(adminService.selectOrderCount());
        Integer pageCount=dataCount%number==0?dataCount/number:dataCount/number+1;
        Page page = new Page(index, dataCount, pageCount, number);
        for(AdminOrderView adminOrderView:adminOrderViews){
            Date addTime = adminOrderView.getAddTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = simpleDateFormat.format(addTime);
            adminOrderView.setTime(format);
        }
        return new Result("200","success",page,adminOrderViews);
    }
    @RequestMapping("selectCoupon")
    public Result selectCoupon(Integer index,Integer number)throws Exception{
        List<Coupon> coupons = adminService.selectAllCoupons(index, number);
        Integer dataCount = Math.toIntExact(adminService.selectCouponCount());
        Integer pageCount=dataCount%number==0?dataCount/number:dataCount/number+1;
        Page page = new Page(index, dataCount, pageCount, number);
        return new Result("200","success",page,coupons);
    }
}
